package com.itheima.sort;

import org.apache.hadoop.io.WritableComparable;

import java.io.DataInput;
import java.io.DataOutput;
import java.io.IOException;

public class SortPojo implements WritableComparable<SortPojo> {

    private String first;
    private String second;

    public SortPojo() {
    }

    @Override
    public String toString() {
        return first+"\t"+second;
    }

    public String getFirst() {
        return first;
    }

    public void setFirst(String first) {
        this.first = first;
    }

    public String getSecond() {
        return second;
    }

    public void setSecond(String second) {
        this.second = second;
    }

    //先按照第一列字典的倒序排列，如果第一列相同，再按照第二列的字典升序排列
    //正数：前比后大
    @Override
    public int compareTo(SortPojo o) {
        int i = o.getFirst().compareTo(this.first);//倒序
        if(i==0){
            int i1 = this.second.compareTo(o.getSecond());//升序
            return i1;
        }else {
            return i;
        }
    }

    //序列化,序列化和反序列化的字段必须保持一致。
    @Override
    public void write(DataOutput dataOutput) throws IOException {
        dataOutput.writeUTF(first);
        dataOutput.writeUTF(second);
    }

    //反序列化的操作
    @Override
    public void readFields(DataInput dataInput) throws IOException {
        this.first = dataInput.readUTF();
        this.second = dataInput.readUTF();
    }
}
